package com.db.dbcloud;

import com.db.adapter.MenuImageAdapter;
import com.db.vo.User;

import net.tsz.afinal.FinalActivity;
import net.tsz.afinal.annotation.view.ViewInject;
import android.app.ActivityGroup;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.LinearLayout;

@SuppressWarnings("deprecation")
public class MainActivity extends ActivityGroup {
	private GridView gridviewToolbar;
	private LinearLayout content;
	
	public static User user;
	
	private MenuImageAdapter menu;
	private int menu_img[] = new int[]{R.drawable.other_files, R.drawable.uplode, R.drawable.setting};
	private int width = 0;
	private int height = 0;
	private Intent intent;
	
	@SuppressWarnings("deprecation")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		this.gridviewToolbar = (GridView)super.findViewById(R.id.main_bar);
		this.content = (LinearLayout) super.findViewById(R.id.main_content);
		
		
		this.gridviewToolbar.setNumColumns(this.menu_img.length);
		this.gridviewToolbar.setGravity(Gravity.CENTER);
		this.gridviewToolbar.setSelector(new ColorDrawable(Color.TRANSPARENT));
		this.gridviewToolbar.setVerticalSpacing(0);
		this.gridviewToolbar.setBackgroundColor(Color.DKGRAY);
		
		this.width = super.getResources().getDisplayMetrics().widthPixels / this.menu_img.length;
		this.height = super.getResources().getDisplayMetrics().heightPixels / 12;
		
		this.menu = new MenuImageAdapter(this, this.menu_img, this.width, this.height, R.drawable.menu_selected);
		this.gridviewToolbar.setAdapter(this.menu);
		this.switchActivity(0);
		
		this.gridviewToolbar.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View v, int position,
					long arg3) {
				MainActivity.this.switchActivity(position);
			}
		});
	}
	private void switchActivity(int id){
		this.menu.setFocus(id);
		this.content.removeAllViews();
		switch(id){
		case 0:
			this.intent = new Intent(MainActivity.this, ServiceActivity.class);
			break;
		case 1:
			break;
		case 2:
			this.intent = new Intent(MainActivity.this, SettingActivity.class);
			break;
		}
		/*Bundle userBundle = new Bundle();
		userBundle.putSerializable("user", user);
		intent.putExtra("user", userBundle);*/
		this.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		Window subActivity = this.getLocalActivityManager().startActivity("subActivity", this.intent);
		this.content.addView(subActivity.getDecorView(), LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
	}
}
